package com.yami.shop.api.controller.tudu;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.tudu.ShoppingCart;
import com.yami.shop.bean.model.tudu.SmallBasket;
import com.yami.shop.dao.tudu.ShoppingCartMapper;
import com.yami.shop.dao.tudu.SmallBasketMapper;
import com.yami.shop.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/p/smallbasket")
@Api(tags = "小购物车接口")
public class SmallBasketController {

        @Autowired
        private SmallBasketMapper smallBasketMapper;
        @Autowired
        private CombinationController combinationController;
        @Autowired
        private ShoppingCartMapper shoppingCartMapper;

    @GetMapping("/query")
    @ApiOperation(value = "查询小购物车商品", notes = "查询小购物车商品，需要传入购买方式")
    public ResponseEntity<List<SmallBasket>> query(Integer mode){
        String userId = SecurityUtils.getUser().getUserId();
        List<SmallBasket> list = smallBasketMapper.selectList(new LambdaQueryWrapper<SmallBasket>()
                .eq(SmallBasket::getUserId,userId)
                .eq(SmallBasket::getMode,mode));
        return ResponseEntity.ok(list);
    }

        @PostMapping("/ins")
        @ApiOperation(value = "添加、修改小购物车商品", notes = "添加、修改小购物车商品")
        public ResponseEntity<Boolean> ins(SmallBasket small){
            String userId = SecurityUtils.getUser().getUserId();
            small.setUserId(userId);
            if(small.getId() != null){
                if(small.getBasketCount() < 1){return ResponseEntity.ok(false);}//修改的商品数量小于1则不可修改
                int res = smallBasketMapper.update(small,new LambdaQueryWrapper<SmallBasket>()
                .eq(SmallBasket::getId,small.getId()));
                if (res == 1){return ResponseEntity.ok(true);}else { return ResponseEntity.ok(false); }
            }else {
                int res = smallBasketMapper.insert(small);
                if (res == 1){return ResponseEntity.ok(true);}else { return ResponseEntity.ok(false); }
            }
    }

    @DeleteMapping("/del")
    @ApiOperation(value = "删除小购物车商品", notes = "删除小购物车商品，传入id")
    public ResponseEntity<Boolean> del(Integer id){
        int res = smallBasketMapper.deleteById(id);
        if (res == 1){return ResponseEntity.ok(true);}else { return ResponseEntity.ok(false); }
    }

    @PostMapping("/insMax")
    @ApiOperation(value = "将选好的商品放入大购物车", notes = "将选好的商品放入大购物车,需要传入购买方式")
    public ResponseEntity<String> insMax(Integer mode){
        String userId = SecurityUtils.getUser().getUserId();
        ArrayList<Integer> lists=new ArrayList<Integer>();
        List<SmallBasket> smalls = smallBasketMapper.selectList(new LambdaQueryWrapper<SmallBasket>()
                .eq(SmallBasket::getUserId,userId)
                .eq(SmallBasket::getMode,mode));//条件查询小购物车商品
        if(mode == 2){//组合购买模式
            for (SmallBasket small:smalls){
                lists.add(small.getId());//添加id到list
            }
            Long[] longs = new Long[lists.size()];//list转为数组
            for (int i = 0;i<lists.size();i++){
                Long k=lists.get(i).longValue();
                longs[i] = k;
            }
            String res = combinationController.zuhe(longs);//组合小购物车商品
            ShoppingCart shop = new ShoppingCart();
            shop.setCommodityId(-1);//-1是组合商品
            shop.setUserId(userId);
            shop.setCommodityType(2);//商品类型为组合
            shop.setMode(2);//购买方式是组合模式
            shop.setCombinationId(res);//组合id
            shop.setBasketCount(1);//购物车产品个数
            int result = shoppingCartMapper.insert(shop);//放入大购物车
            if(result == 1){return ResponseEntity.ok("商品放入购物车成功(组合购买模式)");}else {return ResponseEntity.ok("商品放入购物车失败(组合购买模式)");}
        }else{//组合加单品购买模式
            for (SmallBasket small:smalls){
                ShoppingCart shop = new ShoppingCart();
                shop.setCommodityId(small.getCommodityId());//商品id,-1是组合商品
                shop.setUserId(userId);
                shop.setCommodityType(small.getCommodityType());//商品类型
                shop.setMode(small.getMode());//购买方式
                shop.setCombinationId(small.getCombinationId());//组合id
                shop.setBasketCount(small.getBasketCount());//产品个数
                shoppingCartMapper.insert(shop);//放入大购物车
                smallBasketMapper.deleteById(small.getId());//从小购物车内删除
            }
            return ResponseEntity.ok("所有商品加入购物车完毕");
        }
    }

}
